<?php

/**
 *      [品牌空间] (C)2001-2010 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: goodsearch.php 5329 2010-12-01 10:53:11Z fanshengshuai $
 */

require_once './common.php';
require_once './source/class/image.class.php';

$modules = array('shop', 'goods', 'consume', 'groupbuy', 'photo');

$m = intval($_G['gp_m']);

if ($m > 4) {
	die("Congratulation ! Log: at './data/log/patch_remarkalbum.log.php'");
}

$cur = intval($_G['gp_cur']);

$total = intval($_G['gp_total']);

switch ($modules[$m]) {
	case 'shop':
		$total == 0 ? $total = DB::result_first("SELECT count(*) as count FROM " . DB::table("shopitems") . "") : "";
		$thumb_w = 200;
		$thumb_h = 200;
		break;
	case 'goods':
		$total == 0 ? $total = DB::result_first("SELECT count(*) as count FROM " . DB::table("gooditems") . "") : "";
		$thumb_w = 200;
		$thumb_h = 200;
		break;
	case 'consume':
		$total == 0 ? $total = DB::result_first("SELECT count(*) as count FROM " . DB::table("consumeitems") . "") : "";
		$thumb_w = 210;
		$thumb_h = 130;
		break;
	case 'groupbuy':
		$total == 0 ? $total = DB::result_first("SELECT count(*) as count FROM " . DB::table("groupbuyitems") . "") : "";
		$thumb_w = 210;
		$thumb_h = 130;
		break;
	case 'photo':
		$total == 0 ? $total = DB::result_first("SELECT count(*) as count FROM " . DB::table("photoitems") . "") : "";
		$thumb_w = 120;
		$thumb_h = 120;
		break;
	default:
		break;
}

if ($cur <= $total) {
	$image = new image();
	$query = DB::query("SELECT itemid, subjectimage FROM " . DB::table(($modules[$m] == "goods" ? "good" : $modules[$m]) . "items") . " WHERE itemid >= " . $cur . " AND itemid < " . ($cur + 100));
	while ($item = DB::fetch($query)) {

		if ($item['subjectimage'] && strpos($item['subjectimage'], 'http://') !== 0) {
			$img_src = A_DIR . "/" . $item['subjectimage'];
			@unlink(substr($img_src, 0, -4).".thumb.jpg");
			@unlink($img_src . ".thumb.jpg");
			logecho("=================================" . $modules[$m] . '[' . $item['itemid'] . ']' . '[' . $thumb_w . 'x' . $thumb_h . "]===================================<br />\n");
			logecho("Make Thumb: " . $img_src . "" . "<br />\nSTATUS: ");
			if (file_exists($img_src)) {
				$thumbed = $image->Thumb($img_src, "", $thumb_w, $thumb_h);
				if (!$thumbed) {
					@copy($img_src, $img_src . ".thumb.jpg");
				}
				logecho("<span style=\"color:blue\">**** SUCCESS ****</span><br />\n");
			} else {
				logecho("<span style=\"color:red\">--- FAILDED ---</span><br />\n");
			}
		} else {
			logecho("=================================" . $modules[$m] . '[' . $item['itemid'] . ']' . '[' . $thumb_w . 'x' . $thumb_h . "]===================================<br />\n".
				"Image Path: {$item['subjectimage']}<br />\nSTATUS: ".
				"<span style=\"color:red\">--- IGNORED ---</span><br />\n");
		}
// if ($m ==4 && $item['itemid'] == 200) exit;
	}
	echo "<script type=\"text/javascript\">setTimeout(\"window.location.href = '?m={$m}&total={$total}&cur=" . ($cur + 100) . "';\", 1500);</script>\n";
} else {
	echo "<script type=\"text/javascript\">setTimeout(\"window.location.href = '?m=" . ($m + 1) . "&total=0&cur=0';\", 1500);</script>\n";
}

logecho('', 1);

function logecho($string = '', $end = 0) {
	static $logstring;
	$logstring = (string) $logstring;
	$logstring .= $string;
	echo $string;
	if($end) {
		$logstring = "<?php exit(); ?>\n{$_SERVER['REQUEST_URI']}\n".date('Y-m-d H:i:s')."\n".$logstring;
		remarklog($logstring);
	}
}

function remarklog($string) {
	@$fp = fopen(B_ROOT.'./data/log/patch_remarkalbum.log.php', 'a');
	@flock($fp, 2);
	@fwrite($fp, $string);
	@fclose($fp);
}

?>
